<template>
  <div>
    <UploadExcel :on-success="upload" />
  </div>
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    async upload(data) {
      // console.log(data)
      const newResults = data.results.map(user => {
        return this.zh2en(user)
      })
      // console.log(data.results)
      // console.log(newResults)
      // 用转换后的数据进行请求发送
      await importEmployee(newResults)
      this.$message.success('导入成功')
      this.$router.back()
    },
    zh2en(user) {
      const newUser = {}
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      for (const key in user) {
        const enKey = dict[key]
        if (enKey === 'timeOfEntry' || enKey === 'correctionTime') {
          const timeStr = this.formatDateExcel(user[key], '/')
          newUser[enKey] = new Date(timeStr)
        } else {
          newUser[enKey] = user[key]
        }
      }
      return newUser
    },
    formatDateExcel(num, format = '-') {
      num = Number(num)
      let millisecond = 0
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd
    }

  }
}
</script>

<style>

</style>
